<?php

require_once 'googleApiSrc/apiClient.php';
require_once 'googleApiSrc/contrib/apiCalendarService.php';
require_once 'models/GoogleCalender/GoogleEvent.php';
        
session_start();

$client = new apiClient();
$client->setApplicationName("Google Calendar Sync");

$cal = new apiCalendarService($client);
$calender_id="";
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}    
if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  $calendar = $cal->calendars->get("primary");// Get Primary Calender 
  $calender_id = $calendar->getId();//Set ID of primary Calender
  $_SESSION['calender_id'] = $calender_id;
  $googleevent = new GoogleEvent($client);
  $eventslist = $googleevent->getEventList($calender_id);
  print "<ul> <li><a class='login' href='test_bootstrap.php'>Run Sync</a></li>";
  print "<li><a class='login' href='test_sftogoogleupsert.php'>Run Salesforce to Google Sync</a></li>";
  print "<li><a class='login' href='test_googletosfupsert.php'>Run Google to Salesforce Sync</a></li>";
  print "<li><a class='login' href='main.php?logout=true'>Logout</a></li></ul>";
  print "<h1>Calender Events</h1><pre>" . print_r($eventslist,true) . "<pre>";

$_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

?>